<?php

#<editor-fold defaultstate="collapsed" desc="CCore::Using">
CCore::Using('socket.inc.streams.inc.interface');
#</editor-fold>

class CCoreSocketIncStreamsSocket implements ICoreSocketIncStreams
{
	#<editor-fold defaultstate="collapsed" desc="private">
	/**
	 * @var	resource
	 */
	private $_rSocket;
	
	/**
	 * @var	string
	 */
	private $_sStreamData;
	#</editor-fold>

	/**
	 * @param	string	$sStreamData = ''
	 * @return	CCoreSocketIncStreamsSocket
	 */
	public function __construct($sStreamData = '')
	{
		CCore::CheckTypes(array('$sStreamData'));

		$this->_sStreamData = $sStreamData;
	}

	/**
	 * @param	string	$sHost
	 * @param	int		$iPort
	 * @param	int		&$iErrorNo
	 * @param	string	&$sErrorStr
	 * @param	int		$iConnectTimeOut
	 * @return	bool
	 */
	public function Open($sHost, $iPort, &$iErrorNo, &$sErrorStr, $iConnectTimeOut)
	{
		CCore::CheckTypes(array('$sHost', '$iPort', '$iErrorNo', '$sErrorStr', '$iConnectTimeOut'));

		$this->_rSocket = @fsockopen($sHost, $iPort, $iErrorNo, $sErrorStr, $iConnectTimeOut);
		return is_resource($this->_rSocket);
	}

	/**
	 * @return	void
	 */
	public function Close()
	{
		@fclose($this->_rSocket);
		$this->_rSocket = null;
	}

	/**
	 * @return	bool
	 */
	public function IsConnected()
	{
		return is_resource($this->_rSocket);
	}

	/**
	 * @param	string	$sLine
	 * @return	bool
	 */
	public function Write($sLine)
	{
		CCore::CheckTypes(array('$sLine'));

		return (false !== @fwrite($this->_rSocket, $sLine, strlen($sLine)));
	}

	/**
	 * @return	string | false
	 */
	public function Read()
	{
		return @fgets($this->_rSocket, 512);
	}

	/**
	 * @return	array | false
	 */
	public function Status()
	{
		return @stream_get_meta_data($this->_rSocket);
	}

	/**
	 *
	 * @param	int		$iSocketTimeOut
	 * @param	bool	$bSocketBlocking
	 */
	public function SetTimeAndBlocking($iSocketTimeOut, $bSocketBlocking)
	{
		CCore::CheckTypes(array('$iSocketTimeOut', '$bSocketBlocking'));

		@stream_set_timeout($this->_rSocket, $iSocketTimeOut);
		@stream_set_blocking($this->_rSocket, $bSocketBlocking);
	}
}